export default function updateNodeElement(newElement, virtualDOM, oldVirtualDOM = {}) {
  const props = virtualDOM.props || {}
  const oldProps = oldVirtualDOM.props || {}

  Object.keys(props).forEach(propName => {
    const propValue = props[propName]
    const oldPropValue = oldProps[propName]

    if (propValue !== oldPropValue) {
      if (propName.startsWith("on")) {
        // 事件属性
        const eventName = propName.toLowerCase().slice(2)
        newElement.addEventListener(eventName, propValue)

        // 删除旧事件处理函数
        if (oldPropValue) {
          newElement.removeEventListener(eventName, oldPropValue)
        }

      } else if (propName === "value" || propName === "checked") {
        // 添加value、checked
        newElement[propName] = propValue
      } else if (propName !== "children") {
        if (propName === "className") {
          newElement.setAttribute("class", propValue)
        } else {
          newElement.setAttribute(propName, propValue)
        }
      }
    }
    
  })

  // 判断属性是否被删除
  Object.keys(oldProps).forEach(oldPropName => {
    const value = props[oldPropName]
    if (!value) {
      // 属性被删除了
      if (oldPropName.startsWith("on")) {
        const eventName = oldPropName.toLowerCase().slice(2)
        newElement.removeEventListener(eventName, oldProps[oldPropName])
      } else if (oldPropName !== "children") {
        newElement.removeAttribute(oldPropName)
      }
    }
  })

}